package com.zhichan.kyc_face_plugin;

import android.content.Context;

import androidx.annotation.NonNull;

import com.dtf.ekyc.api.ZIMCallback;
import com.dtf.ekyc.api.ZIMFacade;
import com.dtf.ekyc.api.ZIMFacadeBuilder;
import com.dtf.ekyc.api.ZIMResponse;

import java.util.Map;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** KycFacePlugin */
public class KycFacePlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  private Context mContext;

  private static final String TAG = "kyc_face_plugin_log";

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "kyc_face_plugin");
    channel.setMethodCallHandler(this);
    mContext = flutterPluginBinding.getApplicationContext();
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if (call.method.equals("init")) {
      Log.d(TAG, "enter init.");
      ZIMFacade.install(mContext);
    } else if (call.method.equals("getMetaInfos")) {
      Log.d(TAG, "enter getMetaInfos.");
      String metaInfo = ZIMFacade.getMetaInfos(mContext);
      result.success(metaInfo);
    } else if (call.method.equals("verify")) {
      Log.d(TAG, "enter verify.");
      Map<String, String> params = call.arguments();
      if (params == null) {
        Log.e(TAG, "params is null");
        return;
      }
      String certifyId = params.get("certifyId");
      if (certifyId == null || certifyId.isEmpty()) {
        Log.e(TAG, "certifyId is null");
        return;
      }

      ZIMFacade zimFacade = ZIMFacadeBuilder.create(mContext);
      zimFacade.verify(certifyId, false, new ZIMCallback() {
        @Override
        public boolean response(ZIMResponse response) {
          if (null == response) {
            result.error("-1", "face verify error.", null);
            return false;
          }

          if (1000 == response.code) {
            Log.d(TAG, "face verify success.");
          } else {
            Log.e(TAG, response.code + "," +response.reason);
          }

          result.success(response.code+"");
          return true;
        }
      });
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
